
use crate::services::teacher;
use rocket::{response, Build, Request, Response};
use rocket::form::Form;
use rocket::http::ContentType;
use rocket::response::{content, Responder};
use crate::models::teacher::TeacherModel;
use crate::services::teacher::TeacherService;
use rocket::serde::json::{Json, Value, json};

#[post("/add", format = "json", data = "<param>")]
fn add<'r>(param: Json<TeacherModel>) -> content::RawJson<String> {
    let res = TeacherService::add(param).unwrap();
    let s = serde_json::to_string(&res).unwrap();
    content::RawJson(s.parse().unwrap())
}

#[post("/query", format = "json", data = "<param>")]
fn query<'r>(param: Json<TeacherModel>) -> content::RawJson<String> {
    let teachers = TeacherService::query(param).unwrap();
    let s = serde_json::to_string(&teachers).unwrap();
    content::RawJson(s)
}

#[post("/query_by_pk", format = "json", data = "<param>")]
fn query_by_pk<'r>(param: Json<TeacherModel>) -> content::RawJson<String> {
    let teachers = TeacherService::query_by_pk(param.id.unwrap()).unwrap();
    let s = serde_json::to_string(&teachers).unwrap();
    content::RawJson(s)
}

#[post("/update", format = "json", data = "<param>")]
fn update<'r>(param: Json<TeacherModel>) -> content::RawJson<String> {
    let teachers = TeacherService::update(param).unwrap();
    let s = serde_json::to_string(&teachers).unwrap();
    content::RawJson(s.parse().unwrap())
}

#[post("/delete", format = "json", data = "<param>")]
fn delete<'r>(param: Json<TeacherModel>) -> content::RawJson<String> {
    let teachers = TeacherService::delete(param.id.unwrap()).unwrap();
    let s = serde_json::to_string(&teachers).unwrap();
    content::RawJson(s.parse().unwrap())
}

pub fn mount(rocket: rocket::Rocket<Build>) -> rocket::Rocket<Build> {
    let rocket = rocket.mount("/teacher", routes![add, query, query_by_pk, update, delete]);
    rocket
}

